package service

import (
	"context"
	"webook/follow/domain"
	"webook/follow/repository"
)

type FollowRelationService interface {
	// GetFollowee 哈哈哈哈
	GetFollowee(ctx context.Context, follower, offset, limit int64) ([]domain.FollowRelation, error)
	GetFollower(ctx context.Context, followee, offset, limit int64) ([]domain.FollowRelation, error)
	FollowInfo(ctx context.Context, follower, followee int64) (domain.FollowRelation, error)
	Follow(ctx context.Context, follower, followee int64) error
	CancelFollow(ctx context.Context, follower, followee int64) error
	GetFollowStatic(ctx context.Context, followee int64) (domain.FollowStatics, error)
}

type followRelationService struct {
	repo repository.FollowRepository
}

func NewFollowRelationService(repo repository.FollowRepository) FollowRelationService {
	return &followRelationService{
		repo: repo,
	}
}

// GetFollowee
//
//	@Description: 获取某人的关注列表
//	@param follower 当前用户的 ID
//	@param offset 偏移量
//	@param limit  分页大小
//	@return []domain.FollowRelation 查询的结果
func (svc *followRelationService) GetFollowee(ctx context.Context,
	follower, offset, limit int64) ([]domain.FollowRelation, error) {
	return svc.repo.GetFollowee(ctx, follower, offset, limit)
}

// GetFollower
//
//	@Description: 获取某人的粉丝列表
//	@param followee 当前用户 ID
//	@param offset 偏移量
//	@param limit 分页大小
//	@return []domain.FollowRelation 查询结果
func (svc *followRelationService) GetFollower(ctx context.Context,
	followee, offset, limit int64) ([]domain.FollowRelation, error) {
	return svc.repo.GetFollower(ctx, followee, offset, limit)
}

// FollowInfo
//
//	@Description: 查询某人关注林一个人的详细信息
//	@param follower 关注者 ID
//	@param followee 被关注者 ID
//	@return domain.FollowRelation 查询结果
func (svc *followRelationService) FollowInfo(ctx context.Context, follower, followee int64) (domain.FollowRelation, error) {
	val, err := svc.repo.FollowInfo(ctx, follower, followee)
	return val, err
}

// Follow
//
//	@Description: 点关注
//	@param follower 关注者 ID
//	@param followee 被关注者 ID
func (svc *followRelationService) Follow(ctx context.Context, follower, followee int64) error {
	return svc.repo.AddFollowRelation(ctx, domain.FollowRelation{
		Followee: followee,
		Follower: follower,
	})
}

// GetFollowStatic
//
//	@Description: 查询某人的关注数量以及粉丝数量
//	@param followee 要查询的用户的 ID
//	@return domain.FollowStatics 某用户的注数量以及粉丝数量
func (svc *followRelationService) GetFollowStatic(ctx context.Context, followee int64) (domain.FollowStatics, error) {
	statics, err := svc.repo.GetFollowStatics(ctx, followee)
	if err != nil {
		return domain.FollowStatics{}, err
	}
	return domain.FollowStatics{
		Followers: statics.Followers,
		Followees: statics.Followees,
	}, nil
}

func (svc *followRelationService) CancelFollow(ctx context.Context, follower, followee int64) error {
	return svc.repo.InactiveFollowRelation(ctx, follower, followee)
}
